package cn.wolfcode.wms.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.wms.domain.StockIncomeBill;
import cn.wolfcode.wms.query.StockIncomeBillQueryObject;
import cn.wolfcode.wms.service.IDepotService;
import cn.wolfcode.wms.service.IStockIncomeBillService;
import cn.wolfcode.wms.util.JsonResult;
import cn.wolfcode.wms.util.RequiredPermission;

@Controller
@RequestMapping("stockBill")
public class StockIncomeBillController {

	@Autowired
	private IStockIncomeBillService service;
	@Autowired
	private IDepotService depotService;
	@RequestMapping("list")
	@RequiredPermission("入库单列表")
	public String list(Model model,@ModelAttribute("qo")StockIncomeBillQueryObject qo){
		model.addAttribute("depots", depotService.selectAll());
		model.addAttribute("result", service.query(qo));
		
		return "stockBill/list";
	}
	
	@RequestMapping("delete")
	@RequiredPermission("入库单删除")
	@ResponseBody
	public JsonResult delete(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.deleteByPrimaryKey(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("删除失败");
		}
		return result;
	}
	
	@RequestMapping("input")
	@RequiredPermission("入库单编辑")
	public String input(Model model,Long id){
		model.addAttribute("depots", depotService.selectAll());
		if (id!=null) {
			model.addAttribute("stockBill", service.selectByPrimaryKey(id));
		}
		return "stockBill/input";
	}
	
	@RequestMapping("saveOrUpdate")
	@RequiredPermission("入库单保存或更新")
	@ResponseBody
	public JsonResult saveOrUpdate(StockIncomeBill stockBill){
		JsonResult jsonResult = new JsonResult();
		try {
			if (stockBill.getId()!=null) {
				service.updateByPrimaryKey(stockBill);
			}else {
				service.insert(stockBill);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.markMsg("保存失败");
		}
		return jsonResult;
	}
	
	@RequestMapping("audit")
	@RequiredPermission("入库单审核")
	@ResponseBody
	public JsonResult audit(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.audit(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("审核失败");
		}
		return result;
	}
	
	//查看
	@RequestMapping("show")
	@RequiredPermission("查看入库单详情")
	public String show(Model model,Long id){
		model.addAttribute("stockBill", service.selectByPrimaryKey(id));
		return "stockBill/show";
	}
	
	
}
